<%@page import="support.*, java.util.*, java.sql.*, java.lang.Integer" %>
<%@ page import="java.sql.*, javax.sql.*, javax.naming.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Account</title>
</head>
<body>
   <% 
    String username = request.getUserPrincipal().getName();
    // Connect to ConnectionPool
    Connection conn = null;
    try{
        Context initCtx = new InitialContext();
        DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/CS135");
        conn = ds.getConnection();
    }finally{
    }
    Statement stmt = conn.createStatement();
    
    int applied = 0;
    // checks if account already has an application
    ResultSet rset = stmt.executeQuery("SELECT student FROM applicants WHERE username=\'" + username + "\'");
    if(rset.next()){
        applied = rset.getInt(1);
    }
    %>
    
    <p> Hello
    <b> <%= username %></b></p>
    <p>Role(s):
    <ul>
        <%if (request.isUserInRole("admin")){ %>
            <li>admin</li>
        <% } %>
        <%if (request.isUserInRole("student")){ %>
            <li>student</li>
        <% } %>
        <%if (request.isUserInRole("reviewer")){ %>
            <li>reviewer</li>
        <% } %>
    </ul>

    <!--  displays if they are a student -->
    <%if (request.isUserInRole("student")){ %>
        <p> Application Status:

     <% if(applied == 0){
            out.print("<font color=\"red\">No Application</font>");
        }else{
            out.print("<font color=\"green\">Application Submitted</font>");
        }
    %>
        <p><a href="/Application_Submission/myApplication.do">My Application</a></p>
    <% } %> 
    <!-- displays if they are admin -->
    <%if (request.isUserInRole("admin")){ %>
        <p><a href="/Application_Submission/chair/chairsHome.do">Application Process</a></p>
    <% } %>   
    <!-- hidden variable to keep track of the students pid -->
    <input type="hidden" name="id" value="">
    
    <!-- displays if they are reviewer -->
    <% if(request.isUserInRole("reviewer")){
        // displays all applications in workflow
        ArrayList<ArrayList> workload = (ArrayList<ArrayList>)request.getAttribute("workload");
        out.print("<table border=1>");
        for(int i = 0; i < workload.size(); i++){ 
            out.print("<tr><td>");
            out.print("<a href='displayApplication.do?id=" + workload.get(i).get(0)  + "'>");
            // print first name
            out.print(workload.get(i).get(1) + " ");
            // print middle name
            out.print(workload.get(i).get(2) + " ");
            // print last name
            out.print(workload.get(i).get(3) + "</a><br>"); 
            out.print("</td></tr>");
        }

        out.print("</table>");
    }
    
    %>
    <p><a href="/Application_Submission/invalidate.jsp">Logout</a></p>
</body>
</html>